package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.UniversityDao;
import cn.edu.sdjzu.xg.bysj.domain.Condition;
import cn.edu.sdjzu.xg.bysj.domain.Pagination;
import cn.edu.sdjzu.xg.bysj.domain.University;
import cn.edu.sdjzu.xg.bysj.exception.BysjException;
import cn.edu.sdjzu.xg.bysj.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class UniversityService {
    private static final UniversityDao universityDao = UniversityDao.getInstance();
    private static final UniversityService universityService = new UniversityService();
    //将唯一构造器设置成私有，阻止在本类外生成本类的对象
    private UniversityService(){
    }
    public static UniversityService getInstance(){
        return universityService;
    }
    public Collection<University> findAll(List<Condition> conditionList, Pagination pagination)throws SQLException,BysjException{
        Connection connection = JdbcHelper.getConn();
        Collection<University> universities = universityDao.findAll(connection,conditionList,pagination);
        JdbcHelper.close(connection);
        return universities;
    }
    public String add(University university)throws SQLException ,BysjException{
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        String addMessage = null;
        int universityId = UniversityDao.getInstance().add(connection,university);
        // 为的university的id属性赋值
        university.setId(universityId); // 这行之后university的字段值就完整了
        addMessage = "添加成功";
        connection.commit();
        connection.setAutoCommit(true);
        JdbcHelper.close(null,connection);
        return addMessage;
    }
    public University find(int id)throws SQLException {
        University university = UniversityDao.getInstance().find(id);
        return university;
    }

    public String delete(int id) throws SQLException,BysjException{
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        String deleteMessage = null;
        boolean deleteOrNot = UniversityDao.getInstance().delete(id,connection);
        if(deleteOrNot == true){
            deleteMessage = "删除成功";
        }else {
            deleteMessage = "删除失败，该university不存在";
        }
        connection.commit();
        connection.setAutoCommit(true);
        JdbcHelper.close(null,connection);
        return deleteMessage;
    }

    public String update(University university)throws SQLException , BysjException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        String updateMessage = null;
        boolean updateOrNot = universityDao.getInstance().update(connection,university);
        if(updateOrNot == true){
            updateMessage = "更新成功";
        }else {
            updateMessage = "更新失败，该university不存在";
        }
        connection.commit();
        connection.setAutoCommit(true);
        JdbcHelper.close(null,connection);
        return updateMessage;
    }
}
